package com.dietmanager.view;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import com.dietmanager.model.Exercise;
import com.dietmanager.model.Food;
import com.dietmanager.model.FoodModel;
import com.dietmanager.model.javafx.util.RecipeListEntry;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;

/**
 * 
 * @author Petar, Danijel
 * @description This class is used for creating Exercises which can be added to
 *              Workouts, or used individually. The class functionality is
 *              almost the same as AddFoodView class
 *
 */
public class AddExerciseView extends Observable {

	ArrayList<Observer> observerList = new ArrayList<Observer>();

	private final ObservableList<RecipeListEntry> data = FXCollections
			.observableArrayList();

	public Button add;
	public TextField exerciseNameTextField, caloriesTextField;
	public VBox addExerciseView;

	// main constructor
	public AddExerciseView() {

		createAddExerciseView();
	}

	// Method that creates the interface (GUI)
	public void createAddExerciseView() {
		// Main container
		addExerciseView = new VBox();
		addExerciseView.getStyleClass().add("addExerciseVBox");
		addExerciseView.setSpacing(10);
		addExerciseView.setAlignment(Pos.CENTER);

		// TextFields
		caloriesTextField = new TextField();
		caloriesTextField.setMaxWidth(200);
		caloriesTextField.setPromptText("Exercise Name");

		exerciseNameTextField = new TextField();
		exerciseNameTextField.setMaxWidth(200);
		exerciseNameTextField.setPromptText("Enter Calories");

		// Labels
		Label nameLabel = new Label("Exercise Name:");
		nameLabel.getStyleClass().add("textFieldLabel");
		Label caloriesLabel = new Label("Calories Amount:");
		caloriesLabel.getStyleClass().add("textFieldLabel");

		add = new Button("Add Exercise");
		add.getStyleClass().add("customButton");
		add.setOnAction(new EventHandler<ActionEvent>() {

			// When add button pressed...
			@Override
			public void handle(ActionEvent event) {
				System.out.println(exerciseNameTextField.getText().toString()
						+ " is name, is calorie: "
						+ caloriesTextField.getText());

				// Create and save
				Exercise e = new Exercise(exerciseNameTextField.getText(), 0, 0);
				e.setCalories(Integer.parseInt(caloriesTextField.getText()));
				notifyObservers(e);

				// Empty text fields
				exerciseNameTextField.setText("");
				caloriesTextField.setText("");
			}

		}); // end of add button action

		// Adding items to VBox
		addExerciseView.getChildren().addAll(nameLabel, exerciseNameTextField,
				caloriesLabel, caloriesTextField, add);

		return;
	}// End of GUI procreation

	public VBox getAddExerciseView() {
		return addExerciseView;
	}

	public void addObserver(Observer o) {
		observerList.add(o);
	}

	public void addObserverCollection(ArrayList<Observer> obs) {
		observerList.clear();
		observerList = obs;
	}

	public void notifyObservers(Object o) {
		for (Observer ob : observerList)
			ob.update(this, o);
	}

}// End of class
